/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package entityopinions;

import com.aliasi.chunk.Chunk;
import com.aliasi.chunk.ChunkFactory;
import com.aliasi.chunk.Chunker;
import com.aliasi.chunk.Chunking;
import com.aliasi.chunk.ChunkingImpl;
import com.aliasi.corpus.ChunkHandler;
import com.aliasi.corpus.ObjectHandler;
import com.aliasi.util.Strings;
import com.sleepycat.je.DatabaseException;
import entityopinions.ComputePMI.Kernel;
import java.io.Serializable;
import java.util.Set;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 *
 * @author Diogo
 */
public class LexChunker implements Chunker, Serializable, ObjectHandler<Chunking>{

    public Kernel mKernel;
    public double mSigma;
    public Cache cache;
    public PrintStream output;

    public LexChunker(Kernel k, double sig, String cachePath) throws DatabaseException{
        mKernel = k;
        mSigma = sig;
        File f = new File(cachePath);
        cache = new Cache(f, 100);
    }

    public void setKernel(Kernel k){
        mKernel = k;
    }

    public void setSigma(double sig){
        mSigma = sig;
    }

    @Override
    public Chunking chunk(CharSequence cSeq) {
        char[] cs = Strings.toCharArray(cSeq);
        return chunk(cs, 0, cs.length);
    }

    @Override
    public Chunking chunk(char[] chars, int start, int end) {
        try {
            ChunkingImpl chunking = new ChunkingImpl(chars, start, end);
            int startChunk = 0;
            int endChunk = 0;
            EntityOpinion entop = new EntityOpinion(cache);
            String sentence = new String(chars);
            //String sentenceEntities = entop.entitiesAndSentiment("/home/dsantos/entityopinionsdropboxv2/ngrams_sort", sentence, mKernel, mSigma);
            String sentenceEntities = entop.entitiesAndSentiment("/home/dsantos/WikipediaNgrams", sentence, mKernel, mSigma);
            
            sentenceEntities = sentenceEntities.trim();
            
            //System.out.println("frase dentro do chunk: " + sentenceEntities);
            int startIndex = -1;
            int endIndex = -1;
            while (((startIndex = sentenceEntities.indexOf("<chunk type=")) != -1) && ((endIndex = sentenceEntities.indexOf("</chunk>")) != -1)) {
                String sentiment = sentenceEntities.substring(startIndex + 13, startIndex + 23);
                startChunk += startIndex;
                endChunk += endIndex - 25;
                System.out.println(sentence.substring(startChunk, endChunk));
                Chunk chunk;
                chunk = ChunkFactory.createChunk(startChunk, endChunk, sentiment);

                chunking.add(chunk);
                sentenceEntities = sentenceEntities.substring(endIndex + 8);
                startChunk = endChunk;
            }
            System.out.println(chunking);
            return chunking;
        } catch (Exception ex) {
            Logger.getLogger(LexChunker.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public static void main(String args[]) throws DatabaseException{

        LexChunker l = new LexChunker(Kernel.Gaussian, 1, "/home/dsantos/entityopinionsdropbox/entityTesting/cache");

        //l.chunk("My opinion on a coward like George and Eddie is very negative.");

    }

    @Override
    public void handle(Chunking chnkng) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}











